import requests, re, os

url = 'https://www.yituyu.com/'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
response = requests.get(url, headers=headers)
response.encoding = 'utf-8'

pattern = re.compile(
    r'<a href="https://www\.yituyu\.com/gallery/\d+/".*?'
    r'<img src="(https://img\.yituyu\.com/gallery/\d+/cover\.jpg)".*?'
    r'.*?<div class="chujingren">.*?<a href="https://www\.yituyu\.com/grapher/\d+/">\s*(.*?)\s*</a>',
    re.S
)

matches = pattern.findall(response.text)

for img_url, author in matches:
    # 去掉非法文件名字符
    author = re.sub(r'[\\/:*?"<>|]', '', author)
    print('下载:', img_url, '->', author + '.jpg')

    # 下载图片（带 Referer 防 403）
    img_data = requests.get(img_url,
                            headers={
                                'User-Agent': headers['User-Agent'],
                                'Referer': 'https://www.yituyu.com/'
                            }).content

    # 二进制写盘
    with open(f'{author}.jpg', 'wb') as f:
        f.write(img_data)